package com.aiju.cash.ui.member.adater;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.member.entity.Point;
import com.aiju.utils.DateUtil;
import com.aiju.utils.Util;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-05-19 13:58
 * @version: V1.0
 * @Description: 描述
 */
public class MemberPointAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Point> items;

    public MemberPointAdapter(List<Point> items) {
        this.items = items;
    }

    public int type;

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_point, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        final Point item = items.get(position);
        MyViewHolder holder = (MyViewHolder) viewHolder;
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(item);
                }
            }
        });
        if (type == 0) {
            holder.pointImageView.setImageResource(R.mipmap.ic_point_in);
            holder.moneyTextView.setText("+" + item.getGetPointsValue());
        } else {
            holder.pointImageView.setImageResource(R.mipmap.ic_point_out);
            holder.moneyTextView.setText("-" + item.getUsedPointsValue());
        }
        holder.timeTextView.setText(DateUtil.getStringByFormat(item.getDealTime(), DateUtil.dateFormatYMDHM));
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public boolean isEmpty() {
        return Util.isEmpty(items);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        ImageView pointImageView;
        TextView nameTextView;
        TextView timeTextView;
        TextView moneyTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            pointImageView = (ImageView) itemView.findViewById(R.id.iv_member_point);

            nameTextView = (TextView) itemView.findViewById(R.id.tv_point_type);
            timeTextView = (TextView) itemView.findViewById(R.id.tv_point_time);
            moneyTextView = (TextView) itemView.findViewById(R.id.tv_point_point);
        }
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(Point item);
    }
}
